package com.xuecheng.media.api;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
public class MediaFilesController {


  @Autowired
  MediaFileService mediaFileService;


 @ApiOperation("媒资列表查询接口")
 @PostMapping("/files")
 public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
     Long companyId = 1232141425L;
     return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);
 }

    /**
     * @param filedata
     * @param folder
     * @param objectName
     * @return
     */
 @ApiOperation("文件上传接口")
 @RequestMapping(value = "/upload/coursefile",consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
 public UploadFileResultDto upload(@RequestPart("filedata") MultipartFile filedata,
                                   @RequestParam(value = "folder" ,required = false) String folder,
                                   @RequestParam(value = "objectName",required = false) String objectName){
     Long companyId = 22l;
     UploadFileParamsDto dto = new UploadFileParamsDto();
     String contentType = filedata.getContentType();
     dto.setContentType(contentType);
     dto.setFileSize(filedata.getSize());
     if (contentType.indexOf("image") >= 0){
         //是个图片
         dto.setFileType("001001");
     }else {
         //是个其他
         dto.setFileType("001003");
     }
     dto.setFilename(filedata.getOriginalFilename());
     UploadFileResultDto uploadFileResultDto = null;
     try {
         uploadFileResultDto = mediaFileService.uploadFile(companyId, dto, filedata.getBytes(), folder, objectName);
     }catch (Exception e){
         XueChengPlusException.cast("上传文件出错，请重新上传！");
     }
    return uploadFileResultDto;
 }


    /**
     * 预览文件的接口
     * @param mediaId   文件的id
     * @return   返回url
     */
    @ApiOperation("预览文件")
    @GetMapping("/preview/{mediaId}")
    public RestResponse<String> getPlayUrlByMediaId(@PathVariable String mediaId){

        //调用service来查询文件的url
        MediaFiles fileById = mediaFileService.getFileById(mediaId);
        return RestResponse.success(fileById.getUrl());
    }

}
